/**
 * Created with IntelliJ IDEA.
 * User: teddy zhang
 * Date: 2015-04-17
 * Time: 11:25
 */

define(function (require, exports, module) {
    'use strict';
    
    var rk = require('rk');
    
    require('lib/jquery.validate/loader');

    $.widget('rk.usersettingProfileCtrl', {
        options: {selector: null},

        _create: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            me._render();

        },

        _init: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;

        },


        _render: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;

            var profile_HTML = require('page/tmpl/usersetting/tpl_user_setting_profile.tpl');

            $.postJson('/json/oa_profile-edit/infoIndex.action').done(function (json) {
                if (json.status == 0) {
                    var data = json.data;
                    data.entity.birthday && (data.entity.birthday = moment(parseFloat(data.entity.birthday)).format('YYYY-MM-DD'))
                    data.entity.joinAt && (data.entity.joinAt = moment(parseFloat(data.entity.joinAt)).format('YYYY-MM-DD'))

                    $(elem).html(rk.templateText(profile_HTML, data));
                    $("#radiofomat,#checkfomat").buttonset();

                    me._bind();
                }
            });

        },


        _bind: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;

            $(elem).find('input[entryPropertyNameOnly="birthday"]').datepicker({
                yearRange: "1900:+0"
            });
            $(elem).find('input[entryPropertyNameOnly="joinAt"]').datepicker();

            $(elem).on('click', 'a[act="profile_btn_save"]', function (event) {
                event.preventDefault();
                if (!me._validate())return;
                var userMap = {};
                var profileMap = {};

                $(elem).find('input[entryPropertyNameOnly],textarea').each(function (index, item) {
                    var $item = $(item);
                    if ($item.attr('entryPropertyNameOnly') == 'name') {
                        userMap["userMap['name']"] = $item.val();
                    }else if ($item.attr('entryPropertyNameOnly') == 'gender') {
                        if($item.attr("checked")){
                            profileMap["profileMap['"+$item.attr("entryPropertyNameOnly")+"']"]  =$item.val();
                        }
                    } else if ($item.attr('entryPropertyNameOnly') == 'hiddenYearFlg') {
                        profileMap["profileMap['"+$item.attr("entryPropertyNameOnly")+"']"] = $item.attr("checked") ? '1' : '0';
                    } else {
                        profileMap["profileMap['"+$item.attr("entryPropertyNameOnly")+"']"] = $item.val();

                    }
                });
                 var postData = $.extend({},userMap,profileMap);
                $.postJson('/json/oa_profile-edit/save-base.action', postData).done(function (json) {
                    if (json.status == 0) {
                        var data = json.data;
                        rk.noticeSuccess(rk.i18n('USERSET_SAVED'));
                        opt.selector.trigger('click');
                    }else if(json.status ==  -2){
                        $('input[entryPropertyNameOnly="name"]').select();
                        $("#profile_name_msg").text( rk.i18n('USERSET_USERNAME_EXISTED'));
                        $("html,body").animate({scrollTop: $('input[entryPropertyNameOnly="name"]').position().top}, 1000);
                    }else{
                        rk.noticeError(rk.i18n('USERSET_SYS_ERROR'));
                    }

                }).fail(function (def, code, message) {
                    def.status != 0 && rk.noticeError(rk.i18n('REQUEST_ERROR'));
                });
                return false;
            });


        },


        _validate: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;

            $.validity.start();
            $(".nc_must").require();
            $(".nc_date").match("date");
            $(".nc_integer").match("number");
            $(".nc_phone").match("phone");
            $("#phone").match("mobile");
            $(".nc_email").match("email");
            $("#selfInfo").maxLength(100);
            $("#expertise").maxLength(100);
            $("#hobby").maxLength(100);
            $("#profile_name").match("userName");
            return $.validity.end().valid;


        }

    });

    return $;

});

